package ext.tianma.cascade.util;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.Arrays;
import java.util.Collection;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import ext.generic.erp.config.ERPPartTemplateConfigExcelUtil;
import ext.tianma.cascade.bean.CascadeBean;
import ext.tianma.cascade.config.Config;
import wt.util.WTException;

public class CascadeHelper {

	/**
	 * 级联配置路径
	 */
	private static final String CONFIG_FILE = "物料中文名称和所属部门.xlsx" ;
	public static String CASCADE_PATH1 = Config.class.getResource("").getPath().replaceAll("windchill", "Windchill")+ CONFIG_FILE;
	public static String CASCADE_PATH = "/ptc/Windchill/codebase/ext/tianma/cascade/config/demp.xlsx";
	
//	static {
//		try {
//			CONFIG_FILE = 
////			CASCADE_PATH = (new StringBuilder(String.valueOf(QueryHelper.getClassfilePath(Config.class)))).append("物料中文名称和所属部门.xlsx").toString();
//		} catch(IOException e) {
//			e.printStackTrace();
//		}
//	}
	/**
	 * 项目信息Sheet页名字
	 */
	public static enum CascadeSheetName {
		GROUPSEQUENCENUMBER("所属部门及物料中文名称");

		String value;
		CascadeSheetName(String value) {
			this.value = value;
		}

	}

	public static Workbook getCascadeWorkbook() throws WTException {
		InputStream is = null;
		Workbook wb = null;
		try {
			System.out.println("CASCADE_PATH===>>"+CASCADE_PATH);
			is = new FileInputStream(CASCADE_PATH);
			
			System.out.println("=====>>>"+CASCADE_PATH);
			if (!is.markSupported()) {
				is = new PushbackInputStream(is, 8);
			}

			if (POIFSFileSystem.hasPOIFSHeader(is)) {
				// 2003及以下
				wb = new HSSFWorkbook(is);
			} else if (POIXMLDocument.hasOOXMLHeader(is)) {
				// 2007及以上
				wb = new XSSFWorkbook(is);
			} else {
				// 不是Excel
				throw new WTException("Incorrect configuration file path.");
			}

			return wb;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new WTException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new WTException(e);
		} finally {
			close(is);
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param closeable
	 */
	public static void close(Closeable closeable) {
		try {
			if (closeable != null) {
				closeable.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static CascadeBean getCascade(Workbook workbook, CascadeSheetName sheetName) {
		System.out.println("===>>start==>>action===>>");
		CascadeBean cascade = new CascadeBean();
		Sheet sheet = workbook.getSheet(sheetName.value);
		Row row;
		Cell cell;
		String tempVal;

		// 第一行为表头，从第二行开始遍历
		for (int i = 1, rowNum = sheet.getLastRowNum(); i <= rowNum; i++) {
			if ((row = sheet.getRow(i)) != null) {
				// 遍历列
				int cellNum = row.getLastCellNum();
				String[] cellArray = new String[cellNum + 1];
				for (int j = 0; j <= cellNum; j++) {
					cell = row.getCell(j);
					if (cell == null || (tempVal = cell.getStringCellValue()) == null) {
						cellArray[j] = "";
					} else {
						cellArray[j] = tempVal.trim();
					}
					System.out.println("cellArray=====>>>>>>>>>>>>>>>>"+cellArray);
					cascade.put(cellArray);
				}
			}
		}
		return cascade;
	}


	public static CascadeBean getCascade(CascadeSheetName sheetName, boolean cache) throws WTException {
		System.out.println("cache===>>"+cache);
		if (cache) {
			return CascadeCache.getInstance().get(sheetName);
		} else {
			return getCascade(getCascadeWorkbook(), sheetName);
		}
	}

	public static CascadeBean getCascade(CascadeSheetName sheetName) throws WTException {
		return getCascade(sheetName, true);
	}
	
	/**
	 * 物料中文名称描述
	 */
	public static final String CASCADETYPE_GROUPSNUMBER = "partCNAndENName";

	/**
	 * 所属部门
	 */
	public static final String CASCADETYPE_SEQUENCENUMBER = "departments";
	
	/**
	 * 所属部门
	 */
	public static final String CASCADETYPE_BRANDNAME = "brandName";
	

	public static Collection<String> getCascadeData(CascadeSheetName sheetName, String... keys) throws WTException {
		Collection<String> cascadeData = null;
		CascadeBean bean = getCascade(sheetName);
		System.out.println("bean===>>"+bean);
		if (bean != null) {
			cascadeData = bean.get(keys);
		}
		return cascadeData;
	}
	
	public static Collection<String> getCascadeData(String cascadeType, String... keys) throws WTException {
		return getCascadeData(cascadeType, false, keys);
	}
	
	public static Collection<String> getCascadeData(String cascadeType, boolean obscure, String... keys) throws WTException {
		Collection<String> cascadeData = null;
		System.out.println("cascadeType===>>>"+cascadeType);
		if (CASCADETYPE_GROUPSNUMBER.equals(cascadeType)) {
			cascadeData = getCascadeData(CascadeSheetName.GROUPSEQUENCENUMBER);
			System.out.println("cascadeData===>>>"+cascadeData);
		} else if (CASCADETYPE_SEQUENCENUMBER.equals(cascadeType)) {
			cascadeData = getCascadeData(CascadeSheetName.GROUPSEQUENCENUMBER, obscure ? Arrays.copyOfRange(keys, 0, 1) : keys);
		}  else if (CASCADETYPE_BRANDNAME.equals(cascadeType)) {
			cascadeData = getCascadeData(CascadeSheetName.GROUPSEQUENCENUMBER, obscure ? Arrays.copyOfRange(keys, 0, 1) : keys);
		} else {
			throw new WTException("Parameter is incorrect.");
		}
		return cascadeData;
	}
}
